package cn.itcast.controller;

import cn.itcast.entity.Result;
import cn.itcast.pojo.Address;
import cn.itcast.service.AddressService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.itcast.constant.MessageConstant.QUERY_ADDRESS_FAIL;
import static cn.itcast.constant.MessageConstant.QUERY_ADDRESS_SUCCESS;

@RestController
@RequestMapping(path = "/address")
@CrossOrigin
public class AddressController {

    @Reference
    private AddressService addressService;

    //查询分院列表
    @GetMapping(path = "/findAll")
    public Result findAll() {
        List<Address> list = addressService.findAll();
        try {
            return new Result(true, QUERY_ADDRESS_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, QUERY_ADDRESS_FAIL);
        }
    }

    //新增分院
    @PostMapping(path = "submit")
    public Result submit(@RequestBody Address address){
        if (address.getId()==null){
        return addressService.submit(address);
        }else {
            return addressService.update(address);
        }
    }

    //弹出编辑框
    @GetMapping(path = "/{id}")
    public Result findById(@PathVariable("id") Integer id){
        return addressService.findById(id);
    }

    //删除分院
    @DeleteMapping(path = "/{id}")
    public Result delete(@PathVariable("id") Integer id){
        return addressService.delete(id);
    }

    //启用禁用
    @GetMapping(path = "/{event}/{id}")
    public Result find(@PathVariable("event") Integer event , @PathVariable("id") Integer id){
        return addressService.find(event, id);
    }
}
